import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:bigegg/common/log/xmm_logcat.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:open_file/open_file.dart';
import 'package:rongcloud_im_wrapper_plugin/rongcloud_im_wrapper_plugin.dart';

import '../../../entity/user/xmm_wall_user.dart';
import '../../../res/xmm_img.dart';
import '../../../utils/xmm_string_utils.dart';
import '../../../widget/image/xmm_circle_cached_network_image.dart';

class XmmMediaPreviewPage extends StatelessWidget {
  const XmmMediaPreviewPage({super.key, this.user, this.message});

  final XmmWallUser? user;

  final RCIMIWMediaMessage? message;

  void clickOpen() {
    OpenFile.open(message?.local);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          toolBarWidget(context, user),
          Expanded(child: contentWidget()),
          getBottomWidget(),
        ],
      ),
    );
  }

  Widget getBottomWidget() {
    return GestureDetector(
      onTap: () => clickOpen(),
      child: Container(
        alignment: Alignment.center,
        width: 261,
        height: 52,
        margin: const EdgeInsets.only(top: 15, bottom: 53),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(37),
          color: const Color(0xFF15E8C2),
        ),
        child: const Text(
          "Open",
          style: TextStyle(fontSize: 18, color: Colors.white),
        ),
      ),
    );
  }

  Widget contentWidget() {
    var message = this.message;
    if (message is RCIMIWImageMessage) {
      if (message.local != null && File(message.local ?? "").existsSync()) {
        return Center(
          child: Image.file(File(message.local ?? "")),
        );
      } else if (message.remote != null) {
        return Center(
          child: CachedNetworkImage(
            imageUrl: message.remote ?? "",
          ),
        );
      } else if (message.thumbnailBase64String != null) {
        Uint8List bytes = base64.decode(message.thumbnailBase64String!);
        return Center(
          child: Image.memory(bytes),
        );
      } else {
        return Center(
          child: Image.asset("assets/images/xmm_icon_empty.png"),
        );
      }
    } else if (message is RCIMIWFileMessage) {
      return Container(
        margin: EdgeInsets.only(top: 186.h),
        child: Column(
          children: [
            Image.asset(
              "assets/images/xmm_icon_file.png",
              width: 104,
              height: 104,
            ),
            const SizedBox(height: 10),
            Container(
              padding: const EdgeInsets.only(left: 30, right: 30),
              child: Text(
                message.name ?? "",
                style: const TextStyle(fontSize: 14, color: (Color(0x4D000000))),
              ),
            )
          ],
        ),
      );
    } else {
      return Container();
    }
  }

  Widget toolBarWidget(BuildContext context, XmmWallUser? user) {
    return Container(
      color: const Color(0xFF15E8C2),
      padding: EdgeInsets.only(top: context.mediaQueryPadding.top),
      height: context.mediaQueryPadding.top + 44.w,
      child: Stack(
        alignment: Alignment.center,
        children: [
          Row(
            children: [
              GestureDetector(
                child: Container(
                  margin: const EdgeInsets.only(left: 20),
                  width: 32.w,
                  height: 32.w,
                  padding: EdgeInsets.all(5.w),
                  decoration: const BoxDecoration(
                    shape: BoxShape.circle,
                    color: Colors.white,
                  ),
                  child: Image.asset(XmmImg.backIcon),
                ),
                onTap: () => Get.back(),
              ),
              SizedBox(width: 16.w),
              XmmCircleCachedNetworkImage(
                imageUrl: user?.getAvatarUrl ?? "",
                width: 32.w,
                height: 32.w,
              ),
              SizedBox(width: 8.w),
              Text(
                XmmStringUtils.truncateText(user?.nickname ?? "", 8),
                style: TextStyle(
                  fontSize: 18.sp,
                  color: const Color(0xFF202020),
                ),
                maxLines: 1,
              ),
            ],
          ),
          Positioned(
            top: 7.w,
            right: 74.w,
            child: Image.asset(
              "assets/images/xmm_icon_chat_image.png",
              width: 65.w,
              height: 54.w,
            ),
          ),
        ],
      ),
    );
  }
}
